import java.io.*;


/**
 * <p>Title: FireWall Project. UI Module </p>
 * <p>Description: This part responsible for reading and execute user's FW
 *  commands received via console.
 * <p>Copyright: Copyright (c) 2006</p>
 * @author FW team
 * @version 1.0
 */



public class FW {
	static UI m_ui;

	public static void main(String args[]) {
		m_ui = new UI();
		readAndOperate();
	}

	public static String commandHelp(String commName) throws IOException
	{
		try{
			String helpFile = "help.txt";
			FileInputStream fin = new FileInputStream(helpFile);
			BufferedReader br = new BufferedReader(new InputStreamReader(fin));
			String strLine = new String(); /*read string*/
			int index;
			while ( ( (strLine = br.readLine()) != null) &&
					(!strLine.startsWith(commName + ":"))) {
			}
			if(strLine == null)
			{
				return "No such command defined";
			}
			index = strLine.indexOf(':');
			return strLine.substring(index + 2);
		}
		catch (IOException except){throw except;}

	}

	private static void readAndOperate()
	{
		InputStreamReader isr = new InputStreamReader ( System.in );
		BufferedReader br = new BufferedReader ( isr );
		String strArr[];
		String s = null;

		try {

			System.out.println();
			System.out.println("Type 'Commands' to see the list of all available console's commands");
			System.out.println();
			while ( !(s = br.readLine()).equals("Exit")) {
				try {
					strArr = s.split(" ");
					invokeCommand(strArr);
					}
				catch ( Exception except )
				{
					System.out.println(except);
				}
				System.out.println("******\nEnter next command:");
			}
		}
		catch ( Exception except )
		{
			System.out.println(except);
		}
	}

	private static void invokeCommand(String[] commArr) throws Exception
	{
		String comm = commArr[0];
		try{
			if (comm.equals("Commands")) {
				System.out.println(commandHelp(commArr[0]));
				return;
			}

			if (comm.equals("Start")) {
				m_ui.start();
				return;
			}

			if(comm.equals("Stop")){
				m_ui.stop();
				return;
			}

			if(comm.equals("Help")){
				if(commArr.length == 1 )
				{
					m_ui.help();
					return;
				}
				System.out.println();
				System.out.println(commArr[1] + ":");
				System.out.println();
				System.out.println(commandHelp(commArr[1]));
				System.out.println();
				return;
			}

			if(comm.equals("Save")){
				String fileName = "";
				if(commArr.length > 1){
					fileName = commArr[1];
				}
				m_ui.save(fileName);
				return;
			}

			if (comm.equals("Clean"))
			{
				m_ui.clean();
				return;
			}

			if (comm.equals("Load")) {
				String fileName = "";
				if (commArr.length > 1) {
					fileName = commArr[1];
				}
				m_ui.load(fileName);
				return;
			}

			if(comm.equals("List")){
				m_ui.list();
				return;
			}

			if (comm.equals("ReadLog")) {
			  String fileName = "";
			  if (commArr.length > 1) {
			    fileName = commArr[1];
			  }
			  m_ui.readLog(fileName);
			  return;
	        }


			if(comm.equals("Log")){
				if (commArr.length < 2) {
					System.out.println("Must enter log required state (On/Off)");
					return;
				}
				boolean state = true;
				String stateStr = commArr[1];
				if (!stateStr.equals("Off")&& !stateStr.equals("On"))
				{
					System.out.println("Wrong state was entered.Choose On/Off");
                                        System.out.println("Choose On/Off");
					return;
				}
				if (stateStr.equals("Off")) {
					state = false;
				}
				m_ui.log(state);
				return;
			}

			if (comm.equals("Remove")) {
				boolean direct = true;

				if (commArr.length < 3) {
					System.out.println("Not enough parameters were supplied");
					return;
				}
				int index = Integer.parseInt(commArr[2]);

				String directStr = commArr[1];
				if (!directStr.equals("Out")&& !directStr.equals("In"))
				{
					System.out.println("Wrong direction was entered.");
                                        System.out.println("Choose In/Out.");
					return;
				}
				if (directStr.equals("Out")) {
					direct = false;
				}
				m_ui.remove(direct, index);
				return;
			}

			if(comm.equals("AddIpRule")){
				boolean direct = true;
				boolean perm = true;
				int index = -1;
				String port = "";
				String ip = "";
				int length = commArr.length;
				if(length < 5){
					System.out.println("Not enough parameters were supplied");
					return;
				}
				if(length > 5){
					index = Integer.parseInt(commArr[5]);
				}
				String permStr = commArr[4];
				if (!permStr.equals("Allow")&& !permStr.equals("Deny"))
				{
					System.out.println("Wrong permission was entered");
                                        System.out.println("Choose Allow/Deny");
					return;
				}
				if(permStr.equals("Deny")){
					perm = false;
				}
				String directStr = commArr[3];
				if (!directStr.equals("Out")&& !directStr.equals("In"))
				{
					System.out.println("Wrong direction was entered");
                                        System.out.println("Choose In/Out");
					return;
				}
				if(directStr.equals("Out")){
					direct = false;
				}
				if(!commArr[2].equals("AnyPort")){
					port = commArr[2];
				}
				if(!commArr[1].equals("AnyIp")){
					ip = commArr[1];
				}
				m_ui.addIPRule(ip, port, direct, perm, index);
				return;
			}

			if(comm.equals("AddMatchRule")){
				int length = commArr.length;
				boolean isServer = true;
				if (length < 4) {
					System.out.println("Not enough parameters were supplied");
					return;
				}
				String roleStr = commArr[3];
				if (!roleStr.equals("client")&& !roleStr.equals("server"))
				{
					System.out.println("Wrong role was entered");
                                        System.out.println("Choose client/server");
					return;
				}
				if(roleStr.equals("client"))
					isServer = false;
				m_ui.addMatchRule(commArr[1], commArr[2], isServer);
				return;
			}
			System.out.println("Wrong command. Type 'Commands' to see the list of all available console's commands");
		}
		catch (Exception except) {
			throw except;
		}
	}
}





